package jeecg.business.service.impl.enter;

import java.util.Date;
import java.util.List;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keywordtype.KeywordTypeEntity;
import jeecg.business.service.enter.EnterServiceI;
import jeecg.system.pojo.base.TSDepart;
import jeecg.system.pojo.base.TSRole;
import jeecg.system.pojo.base.TSRoleUser;
import jeecg.system.pojo.base.TSUser;
import jeecg.system.service.UserService;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("enterService")
@Transactional
public class EnterServiceImpl extends CommonServiceImpl implements EnterServiceI {
	
	@Autowired
	UserService userService;
	
	String ENTER_ROLE_NAME = "enteruser";
	
	String ENTER_DEPT_ID = "153";
	
	String MOREN_PASSWORD = "123456";
	
	public List<EnterEntity> list(){
		String hql = " from EnterEntity ";
		return findByQueryString(hql);
	}
	
	public void saveEnter(EnterEntity enter){
		if(getEnter(enter.getAccount())!=null) return ;
		enter.setCreatetime(new Date());
		TSUser u = null;
		TSRoleUser ru = null;
		
		TSDepart dept = get(TSDepart.class, ENTER_DEPT_ID); 
		
		TSRole r = get(TSRole.class, ENTER_ROLE_NAME);		
		
		save(enter);		
		//TSBaseUser
		u = new TSUser();
		u.setTSDepart(dept);
		u.setUserName(enter.getAccount());
		u.setRealName(enter.getName());
		u.setUsertype("2");
		u.setPassword(PasswordUtil.encrypt(u.getUserName(), MOREN_PASSWORD, PasswordUtil.getStaticSalt()));
		u.setMobilePhone(enter.getMobile());
		save(u);
		
		ru = new TSRoleUser();
		ru.setTSRole(r);
		ru.setTSUser(u);
		save(ru);
		
		/**
		 * 保存默认分类
		 */
		KeywordTypeEntity type = new KeywordTypeEntity();
		type.setSxh(1);
		type.setLxmc("默认分组");
		type.setQyid(enter.getId());
		save(type);
	}
	
	public void updateEnter(EnterEntity enter){
		TSUser u = userService.queryUserByName(enter.getAccount());
		TSDepart dept = get(TSDepart.class, ENTER_DEPT_ID);
		u.setTSDepart(dept);
		u.setMobilePhone(enter.getName());
		u.setRealName(enter.getName());
		save(u);
		save(enter);
	}
	
	public void deleteEnter(EnterEntity enter){
		String account = enter.getAccount();
		String sql = " delete from t_s_user where id in (select id from t_s_base_user where username = '"+account+"')";
		String usql = " delete from t_s_base_user where username = '"+account+"'";
		getJdbcTemplate().execute(sql);
		getJdbcTemplate().execute(usql);
		delete(enter);
	}
	
	public EnterEntity getEnter(String account){
		List<EnterEntity> tlist = findByQueryString(" from EnterEntity where account = '"+account+"'");
		if(tlist != null && tlist.size()>0) return tlist.get(0);
		return null;
	}
	
	public List<EnterEntity> listHasLogined(){
		List<EnterEntity> tlist = findByQueryString(" from EnterEntity where loginTime != null and email != null ");
		return tlist;
	}
	
	
}